﻿(function () {
    'use strict';

    var share;
    var sharedText = "";
    function saveCode() {
        var localFolder = Windows.Storage.ApplicationData.current.localFolder;
        localFolder.createFileAsync("code.txt", Windows.Storage.CreationCollisionOption.openIfExists).then(function (dataFile) {
            dataFile.openAsync(Windows.Storage.FileAccessMode.readWriteUnsafe).then(function (stream) {
                var writer = new Windows.Storage.Streams.DataWriter(stream.getOutputStreamAt(0));
                writer.writeString(document.getElementById('shareText').value);
                writer.storeAsync().then(function () {
                });
            });
        });
    }

    function activated(e) {
        if (e.kind === Windows.ApplicationModel.Activation.ActivationKind.shareTarget) {
            share = e.shareOperation;
            document.getElementById('submit').addEventListener('click', onShareSubmit, false);
            document.getElementById('close').addEventListener('click', function () {
                share.reportCompleted();
            }, false);
            document.getElementById('clearCache').addEventListener('click', function () {
                var localFolder = Windows.Storage.ApplicationData.current.localFolder;
                localFolder.createFileAsync("code.txt", Windows.Storage.CreationCollisionOption.openIfExists).then(function (dataFile) {
                    dataFile.deleteAsync().then(function () {
                        console.log("Cache cleared");
                    });
                });
                document.getElementById('shareText').value = sharedText;
            }, false);

            var savedCode = "";

            try {
                sharedText = share.data.getText();
            } catch (e) {
                sharedText = "";
            }

            var localFolder = Windows.Storage.ApplicationData.current.localFolder;
            localFolder.createFileAsync("code.txt", Windows.Storage.CreationCollisionOption.openIfExists).then(function (dataFile) {
                dataFile.openAsync(Windows.Storage.FileAccessMode.read).then(function (stream) {
                    var size = stream.size;
                    if (size > 0) {
                        var inputStream = stream.getInputStreamAt(0);
                        var reader = new Windows.Storage.Streams.DataReader(inputStream);
                        reader.loadAsync(size).then(function () {
                            savedCode = reader.readString(size);
                            document.getElementById('shareText').value = savedCode + "\n" + sharedText;
                            saveCode();
                        });
                    }else {
                        saveCode();
                    }
                });
            });

            document.querySelector('.metadata h1').textContent = share.data.properties.title;
            document.querySelector('.metadata p span').textContent = share.data.properties.description;
            document.getElementById('shareText').value = sharedText;
        }
    }

    function onShareSubmit() {
        try {
            var output = eval(document.getElementById("shareText").innerText, window);
            output && console.log(output);
        } catch (e) {
            console.error(e.description);
        }
    }

    Windows.UI.WebUI.WebUIApplication.addEventListener('activated', activated);
    Windows.UI.WebUI.WebUIApplication.addEventListener("", function () {
    });
})();